package io.practice;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Practice01 {

    public static void main(String[] args) throws IOException {
        try(OutputStream os = new FileOutputStream("test.txt")){
            String s = "wang shuang xi jiu shi wo!";
            byte[] bytes = s.getBytes();
            os.write(bytes);
            os.flush();
        }

        try (OutputStream os = new FileOutputStream("test.txt")){
            String s = "王双喜是中国人！";
            byte[] bytes = s.getBytes("UTF-8");
            os.write(bytes);
            os.flush();
        }
    }
    //一个字节一个字节的写入
    public static void test5() throws IOException{
        try(OutputStream os = new FileOutputStream("test.txt")){
            os.write((byte)'H');
            os.write((byte)'e');
            os.write((byte)'l');
            os.write((byte)'l');
            os.write((byte)'o');
            os.flush();
        }
    }

    //使用Scanner读取文件流
    public static void test4() throws IOException{
        try(InputStream is = new FileInputStream("dest.txt")){
            try(Scanner scanner = new Scanner(is, "UTF-8")){
                while(scanner.hasNext()){
                    String s = scanner.nextLine();
                    System.out.println(s);
                }
            }
        }
    }

    //读取多个字节，不能读取汉字，否则会报异常
    public static void test3() throws IOException {
        try(InputStream is = new FileInputStream("dest.txt")){
            byte[] bytes = new byte[1024];
            int len = 0;

            while(true){
                len = is.read(bytes);
                if(len == -1){
                    break;
                }

                for(int i = 0; i < len; i++){
                    System.out.printf("%c", bytes[i]);
                }
            }
            System.out.println();
        }
    }

    //一个字节一个字节的读取，不能读取汉字，读取后输出的是乱码
    public static void test2() throws IOException {
        File file = new File("dest.txt");
        try(InputStream is = new FileInputStream(file)){
            while(true){
                int b = is.read();
                if(b == -1){
                    //代表文件内容已经全部读完
                    break;
                }
                System.out.printf("%c", b);
            }
        }
        System.out.println();

    }

    public static void test1() throws IOException {
        File file = new File(".\\test.txt");
        System.out.println(file.getName());             //父目录路径
        System.out.println(file.getParent());           //文件名
        System.out.println(file.getPath());             //文件路径
        System.out.println(file.getAbsoluteFile());     //未修饰的绝对路径
        System.out.println(file.getCanonicalPath());    //修饰的绝对路径
        System.out.println(file.isFile());          //是否是文件
        System.out.println(file.isDirectory());     //是否是目录
        System.out.println(file.exists());          //文件是否存在
        System.out.println(file.createNewFile());   //创建文件
        //System.out.println(file.delete());          //删除文件

        File file1 = new File("D:\\Java\\java-ee_code_116\\Javaee");
        //
        String[] filesStr = file1.list();
        System.out.println(Arrays.toString(filesStr));
        File[] files = file1.listFiles();
        for(File file2 : files){
            System.out.println(file2.getName());
        }

        File file2 = new File(file1, "test\\aaa\\bbb");
        System.out.println(file2.mkdir());
        System.out.println(file2.mkdirs());
        System.out.println(file2.isDirectory());
        System.out.println(file2.canRead());
        System.out.println(file2.canWrite());
        //System.out.println(file2.delete());

        File file3 = new File("dest.txt");
        System.out.println(file.renameTo(file3));
    }
}
